#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "zkCompress.h"
#include <QStandardPaths>
#include <time.h>
#include <QFileDialog>
#include <Windows.h>
#include <QDebug>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->progressBar->hide();
    ui->progressBar->setValue(0);
    ui->progressBar->setMaximum(100);
    ui->widget_finish->hide();


    ui->progressBar_2->hide();
    ui->progressBar_2->setValue(0);
    ui->progressBar_2->setMaximum(100);
    ui->widget_finish_2->hide();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::slot_compressProgress(QString fileName ,int currentIndex, int allIndex)
{
    ui->progressBar->show();
    int precent = currentIndex *100 / allIndex;
    if(precent== 100)
    {
        ui->widget_finish->show();
        ui->progressBar->hide();
    }
    qDebug() <<  fileName << QString("compress: %1%").arg(QString::number(precent));

    ui->progressBar->setValue(currentIndex);
    ui->progressBar->setMaximum(allIndex);
}

void MainWindow::slot_extractProgress(QString fileName ,int currentIndex, int allIndex)
{
    ui->progressBar_2->show();
    int precent = currentIndex *100 / allIndex;
    if(precent== 100)
    {
        ui->widget_finish_2->show();
        ui->progressBar_2->hide();
    }
    qDebug() <<  fileName << QString("extract: %1%").arg(QString::number(precent));

    ui->progressBar_2->setValue(currentIndex);
    ui->progressBar_2->setMaximum(allIndex);
}


void MainWindow::on_pushButton_2_clicked()  //压缩文件夹
{
    QString dir_to_zip = ui->textEdit->toPlainText();

    dir_to_zip.replace("/","\\");
    QString dir_name = dir_to_zip.split(QDir::separator()).last();
//        QString tmp_zip = QStandardPaths::writableLocation(QStandardPaths::TempLocation)+ QString("/%1_%2.zip").arg(dir_name).arg(QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss"));
    QString tmp_zip = QString("D:/%1_%2.zip").arg(dir_name).arg(QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss"));
    ui->label_zip_path->setText(tmp_zip);
    qDebug()<< dir_to_zip << tmp_zip;

    zkCompressThread *thread= new zkCompressThread(zkCompressThread::compress_Dir,tmp_zip,dir_to_zip);
    connect(thread,SIGNAL(sig_compressProgress(QString,int,int)),this,SLOT(slot_compressProgress(QString,int,int)));
    thread->start();

}

void MainWindow::on_pushButton_clicked()
{
    QString dir_to_zip = QFileDialog::getExistingDirectory(NULL,"new_path_tips_2","/");
    if(dir_to_zip!="")
    {
        dir_to_zip.replace("/","\\");
        ui->textEdit->setText(dir_to_zip);
    }
}

void MainWindow::on_pushButton_3_clicked()
{
    qDebug() << ui->label_zip_path->text();
    ShellExecute(NULL, L"open", L"Explorer.exe", QString("/select, %1").arg(ui->label_zip_path->text()).toStdWString().c_str(), NULL, SW_SHOWNORMAL);
}

void MainWindow::on_pushButton_5_clicked()  //解压
{
    ui->widget_finish_2->hide();
    QString zip_path = ui->lineEdit->text();
    QString dest_dir_path = ui->label_dir_path->text();

    zip_path.replace("/","\\");
    dest_dir_path.replace("/","\\");

    qDebug()<< zip_path << dest_dir_path;

    zkCompressThread *thread= new zkCompressThread(zkCompressThread::extract_Dir,zip_path,dest_dir_path);
    connect(thread,SIGNAL(sig_extractProgress(QString,int,int)),this,SLOT(slot_extractProgress(QString,int,int)));
    thread->start();
}

void MainWindow::on_pushButton_4_clicked()
{
    QString dir_to_zip = QFileDialog::getOpenFileName(NULL,"new_path_tips_2","/");
    if(dir_to_zip!="")
    {
        dir_to_zip.replace("/","\\");
        ui->lineEdit->setText(dir_to_zip);

        QString dir_name = dir_to_zip.split(QDir::separator()).last();
        dir_name = dir_name.mid(0, dir_name.lastIndexOf("."));
        QString dir_current_path = dir_to_zip.mid(0, dir_to_zip.lastIndexOf(QDir::separator()));
        dir_current_path = QString("%1\\%2_%3").arg(dir_current_path).arg(dir_name).arg(QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss"));
        ui->label_dir_path->setText(dir_current_path);
    }
}

void MainWindow::on_pushButton_7_clicked()
{
    qDebug() << ui->label_dir_path->text();
    ShellExecute(NULL, L"open", L"Explorer.exe", QString("/select, %1").arg(ui->label_dir_path->text()).toStdWString().c_str(), NULL, SW_SHOWNORMAL);

}
